package com.zens.cms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.zens.cms.entity.TColumn;
import com.zens.cms.request.ArticleQueryRequest;
import com.zens.cms.response.ColumnLinkResponse;
import com.zens.cms.response.JsonResponse;
import com.zens.cms.services.IArticle;

/**
 * 
 * @author zoe.C 
 * @createTime 
 * @remark
 */
@Controller
@RequestMapping("/main")
public class ArticleChildController {

	@Resource
	IArticle iArticle;

	@RequestMapping("/columnLinkSiteSource")
	public String columnLinkSiteSource(Model model){
		List<ColumnLinkResponse> listRes = new ArrayList<ColumnLinkResponse>();
		List<ColumnLinkResponse> tempList = null;
		List<Long> cidList = new ArrayList<Long>();
		JSONObject json = new JSONObject();
		
		List<TColumn> colList = iArticle.getTcolForFColumnUrl();
		
		JSONArray arrs;
		JSONObject jsons;
		JSONArray array;
		JSONObject obj;
		ArticleQueryRequest ar;
		if(colList.size() > 0){
			for(int i = 0,len = colList.size();i < len; i ++){
				ColumnLinkResponse cr = new ColumnLinkResponse();
				
				ar = new ArticleQueryRequest();
				cidList.clear();
				cidList.add(colList.get(i).getId());
				ar.setTcolidArr(cidList);
				tempList = iArticle.getLinkSource(ar);
				cr.setColId(colList.get(i).getId());
				cr.setColumnName(tempList.get(0).getColumnName());
				cr.setWebSiteName(tempList.get(0).getWebSiteName());
				
				arrs = JSONArray.fromObject("[" + colList.get(i).getFcolumnurl() + "]");
				
				for(int j = 0; j < arrs.size();j++){
					jsons = JSONObject.fromObject(arrs.get(j).toString());
					if(!jsons.isNullObject() && jsons.getString("name").toString().intern() == "Link".intern()){
						array = jsons.getJSONArray("data");
						obj = (JSONObject)array.get(0);
						
						cidList.clear();
						cidList.add(((JsonResponse)JSONObject.toBean(obj,JsonResponse.class)).getTARGET());
						ar.setTcolidArr(cidList);
						tempList = iArticle.getLinkSource(ar);
						cr.setLinkcolId(tempList.get(0).getLinkcolId());
						cr.setLinkColName(tempList.get(0).getColumnName());
						cr.setLinkWebSiteName(tempList.get(0).getWebSiteName());
					}
				}
				
				listRes.add(cr);
			}
		}
		
		json.put("source", listRes);
		model.addAttribute("result", json.toString());
		return "/json";
	}
	
	@RequestMapping("/cancleColumnLinkForSite")
	public String cancleColumnLinkForSite(Model model,String cidStr,String linkStr){
		String[] cidArr = cidStr.split(";"); 
		String[] linkArr = linkStr.split(";");
		int res = 0;			
		for(int i = 0,len = cidArr.length;i < len;i ++){
			if(StringUtils.hasText(cidArr[i])){
				TColumn col = new TColumn();
				col.setId(Long.valueOf(cidArr[i]));
				col.setFcolumnurl("{\"name\":\"Link\",\"data\":[{\"TARGET\":\""+ linkArr[i] +"\"}]},");
				res += iArticle.updateColumnLink(col);
			}
		}
		model.addAttribute("result", res);
		return "/json";
	}
	
	@RequestMapping("/columnByIdForICO")
	public String columnByIdForICO(Model model,Long cid){
		model.addAttribute("result", new Gson().toJson(iArticle.getTcolumnByTid(cid)));
		return "/json";
	}
}
